package org.webscale.json.cmd;
import org.apache.log4j.Logger;
import org.webscale.ApplicationController;
import org.webscale.json.JsonRequestHandler;

import com.google.gson.Gson;


public class JsonCmdHandler implements JsonRequestHandler{
	private static final Logger log = Logger.getLogger(JsonCmdHandler.class);
	

	public String executeRequest(String jsonRequestString) {
		log.debug("JSON String received-"+ jsonRequestString);
		Gson gson = new Gson();
		JsonCmd cmd = gson.fromJson(jsonRequestString, JsonCmd.class);
		JsonCommandRegistry registry = (JsonCommandRegistry)ApplicationController.instance().locateObject("jsonCommandRegistry");
		AppEngineCmd acmd =registry.getAppEngineCmd(cmd.getName().trim());
		
		// execute any switches
//		acmd.setSwitches(cmd.getSwitches());
		AppEngineCmdResults results = acmd.execute(cmd.getParameters());
		gson = new Gson();
		String resultJson = gson.toJson(results);
		return resultJson;
	}

	
	
}
